#include "interface.h"
#include "graph.h"
#include <gr_graph_fun.h>

enum {COLOR_NODE,COLOR_EDGE,COLOR_LABEL};

static GnomeUIInfo menu1_uiinfo[] =
{
  {
    GNOME_APP_UI_ITEM, "Node",
    "Set Node Color",
    (gpointer) activateColorNode, NULL, NULL,
    GNOME_APP_PIXMAP_NONE, NULL,
    0, (GdkModifierType) 0, NULL
  },
  {
    GNOME_APP_UI_ITEM, "Edge",
    "Set edge color",
    (gpointer) activateEdgeColor, NULL, NULL,
    GNOME_APP_PIXMAP_NONE, NULL,
    0, (GdkModifierType) 0, NULL
  },
  {
    GNOME_APP_UI_ITEM, "Label",
    "Set the item node label color",
    (gpointer) activateLabelColor, NULL, NULL,
    GNOME_APP_PIXMAP_NONE, NULL,
    0, (GdkModifierType) 0, NULL
  },
  GNOMEUIINFO_END
};

GtkWidget*
create_color_select (GtkWidget *graph)
{
  GtkWidget *color_select;
  GtkWidget *vbox18;
  GtkWidget *hbox18;
  GtkWidget *colorSelection;
  GtkWidget *vseparator1;
  GtkWidget *vbox19;
  GtkWidget *table1;
  GtkWidget *labelNodeColor;
  GtkWidget *labelEdge;
  GtkWidget *labelLabel;
  GtkWidget *entryNodeColor;
  GtkWidget *entryEdgeColor;
  GtkWidget *entryLabelColor;
  GtkWidget *optionmenu1;
  GtkWidget *menu1;
  GtkWidget *hbuttonbox7;
  GtkWidget *buttonCancel;
  GtkWidget *buttonOK;
  short *edit_color;
  
  if(!(edit_color = (short *)malloc(sizeof(short))))
	  perror_exit("%s:%d ",__FILE__,__LINE__);
  *edit_color = COLOR_NODE;
  
  color_select = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (color_select), "w3live - Color Selection");
  gtk_window_set_position (GTK_WINDOW (color_select), GTK_WIN_POS_MOUSE);
  gtk_window_set_resizable (GTK_WINDOW (color_select), FALSE);
  gtk_window_set_destroy_with_parent (GTK_WINDOW (color_select), TRUE);

  menu1_uiinfo[0].user_data = (void *)color_select;
  menu1_uiinfo[1].user_data = (void *)color_select;
  menu1_uiinfo[2].user_data = (void *)color_select;
  
  vbox18 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox18);
  gtk_container_add (GTK_CONTAINER (color_select), vbox18);

  hbox18 = gtk_hbox_new (FALSE, 10);
  gtk_widget_show (hbox18);
  gtk_box_pack_start (GTK_BOX (vbox18), hbox18, FALSE, FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (hbox18), 10);

  colorSelection = gtk_color_selection_new ();
  gtk_widget_show (colorSelection);
  gtk_box_pack_start (GTK_BOX (hbox18), colorSelection, FALSE, TRUE, 0);
  gtk_color_selection_set_has_opacity_control (GTK_COLOR_SELECTION (colorSelection), TRUE);
  gtk_color_selection_set_has_palette (GTK_COLOR_SELECTION (colorSelection), TRUE);
  gtk_color_selection_set_update_policy((GtkColorSelection *)colorSelection,GTK_UPDATE_DELAYED);

  vseparator1 = gtk_vseparator_new ();
  gtk_widget_show (vseparator1);
  gtk_box_pack_start (GTK_BOX (hbox18), vseparator1, FALSE, FALSE, 0);

  vbox19 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox19);
  gtk_box_pack_start (GTK_BOX (hbox18), vbox19, TRUE, TRUE, 0);

  table1 = gtk_table_new (3, 2, FALSE);
  gtk_widget_show (table1);
  gtk_box_pack_start (GTK_BOX (vbox19), table1, TRUE, TRUE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (table1), 5);
  gtk_table_set_row_spacings (GTK_TABLE (table1), 5);
  gtk_table_set_col_spacings (GTK_TABLE (table1), 5);

  labelNodeColor = gtk_label_new ("node");
  gtk_widget_show (labelNodeColor);
  gtk_table_attach (GTK_TABLE (table1), labelNodeColor, 0, 1, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelNodeColor), GTK_JUSTIFY_RIGHT);
  gtk_misc_set_alignment (GTK_MISC (labelNodeColor), 0, 0.5);

  labelEdge = gtk_label_new ("edge");
  gtk_widget_show (labelEdge);
  gtk_table_attach (GTK_TABLE (table1), labelEdge, 0, 1, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelEdge), GTK_JUSTIFY_RIGHT);
  gtk_misc_set_alignment (GTK_MISC (labelEdge), 0, 0.5);

  labelLabel = gtk_label_new ("Label");
  gtk_widget_show (labelLabel);
  gtk_table_attach (GTK_TABLE (table1), labelLabel, 0, 1, 2, 3,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelLabel), GTK_JUSTIFY_RIGHT);
  gtk_misc_set_alignment (GTK_MISC (labelLabel), 0, 0.5);

  entryNodeColor = gtk_entry_new_with_max_length (10);
  gtk_widget_show (entryNodeColor);
  gtk_table_attach (GTK_TABLE (table1), entryNodeColor, 1, 2, 0, 1,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_entry_set_editable (GTK_ENTRY (entryNodeColor), FALSE);
  gtk_entry_set_width_chars (GTK_ENTRY (entryNodeColor), 10);

  entryEdgeColor = gtk_entry_new_with_max_length (10);
  gtk_widget_show (entryEdgeColor);
  gtk_table_attach (GTK_TABLE (table1), entryEdgeColor, 1, 2, 1, 2,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_entry_set_editable (GTK_ENTRY (entryEdgeColor), FALSE);
  gtk_entry_set_width_chars (GTK_ENTRY (entryEdgeColor), 10);

  entryLabelColor = gtk_entry_new_with_max_length (10);
  gtk_widget_show (entryLabelColor);
  gtk_table_attach (GTK_TABLE (table1), entryLabelColor, 1, 2, 2, 3,
                    (GtkAttachOptions) (0),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_entry_set_editable (GTK_ENTRY (entryLabelColor), FALSE);
  gtk_entry_set_width_chars (GTK_ENTRY (entryLabelColor), 10);

  optionmenu1 = gtk_option_menu_new ();
  gtk_widget_show (optionmenu1);
  gtk_box_pack_start (GTK_BOX (vbox19), optionmenu1, FALSE, FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (optionmenu1), 5);

  menu1 = gtk_menu_new ();
  gnome_app_fill_menu (GTK_MENU_SHELL (menu1), menu1_uiinfo,
                       NULL, FALSE, 0);

  gtk_option_menu_set_menu (GTK_OPTION_MENU (optionmenu1), menu1);

  hbuttonbox7 = gtk_hbutton_box_new ();
  gtk_widget_show (hbuttonbox7);
  gtk_box_pack_start (GTK_BOX (vbox18), hbuttonbox7, FALSE, TRUE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (hbuttonbox7), 10);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox7), GTK_BUTTONBOX_END);
  gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbuttonbox7), 10);

  buttonCancel = gtk_button_new_from_stock ("gtk-cancel");
  gtk_widget_show (buttonCancel);
  gtk_container_add (GTK_CONTAINER (hbuttonbox7), buttonCancel);
  GTK_WIDGET_SET_FLAGS (buttonCancel, GTK_CAN_DEFAULT);

  buttonOK = gtk_button_new_from_stock ("gtk-ok");
  gtk_widget_show (buttonOK);
  gtk_container_add (GTK_CONTAINER (hbuttonbox7), buttonOK);
  GTK_WIDGET_SET_FLAGS (buttonOK, GTK_CAN_DEFAULT);

  gtk_signal_connect (GTK_OBJECT (buttonCancel), "clicked",
                             GTK_SIGNAL_FUNC (gtk_widget_destroy),
                             GTK_OBJECT (color_select));
  gtk_signal_connect (GTK_OBJECT (buttonOK), "clicked",
                      GTK_SIGNAL_FUNC (set_color),
                      (void *)color_select);
  gtk_signal_connect (GTK_OBJECT(colorSelection), "color-changed",
		  					GTK_SIGNAL_FUNC (load_color),
		  					(void *)color_select);

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (color_select, color_select, "color_select");
  GLADE_HOOKUP_OBJECT_NO_REF (color_select, (void *)edit_color, "edit_color");
  GLADE_HOOKUP_OBJECT_NO_REF (color_select, (void *)graph, "graph");
  GLADE_HOOKUP_OBJECT (color_select, vbox18, "vbox18");
  GLADE_HOOKUP_OBJECT (color_select, hbox18, "hbox18");
  GLADE_HOOKUP_OBJECT (color_select, colorSelection, "colorSelection");
  GLADE_HOOKUP_OBJECT (color_select, vseparator1, "vseparator1");
  GLADE_HOOKUP_OBJECT (color_select, vbox19, "vbox19");
  GLADE_HOOKUP_OBJECT (color_select, table1, "table1");
  GLADE_HOOKUP_OBJECT (color_select, labelNodeColor, "labelNodeColor");
  GLADE_HOOKUP_OBJECT (color_select, labelEdge, "labelEdge");
  GLADE_HOOKUP_OBJECT (color_select, labelLabel, "labelLabel");
  GLADE_HOOKUP_OBJECT (color_select, entryNodeColor, "entryNodeColor");
  GLADE_HOOKUP_OBJECT (color_select, entryEdgeColor, "entryEdgeColor");
  GLADE_HOOKUP_OBJECT (color_select, entryLabelColor, "entryLabelColor");
  GLADE_HOOKUP_OBJECT (color_select, optionmenu1, "optionmenu1");
  GLADE_HOOKUP_OBJECT (color_select, menu1, "menu1");
  GLADE_HOOKUP_OBJECT (color_select, menu1_uiinfo[0].widget, "colorNode");
  GLADE_HOOKUP_OBJECT (color_select, menu1_uiinfo[1].widget, "colorEdge");
  GLADE_HOOKUP_OBJECT (color_select, menu1_uiinfo[2].widget, "colorLabel");
  GLADE_HOOKUP_OBJECT (color_select, hbuttonbox7, "hbuttonbox7");
  GLADE_HOOKUP_OBJECT (color_select, buttonCancel, "buttonCancel");
  GLADE_HOOKUP_OBJECT (color_select, buttonOK, "buttonOK");

  return color_select;
}

void
activateColorNode                      (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	GtkWidget *color = (GtkWidget *)user_data;
	GtkColorSelection *colorsel = (GtkColorSelection *)lookup_widget(color,"colorSelection");
	GtkWidget *graph = (GtkWidget *)lookup_widget(color,"graph");
	graph_options *opts = (graph_options *)lookup_widget(graph,"graph_opts");
	short *ec = (short *)lookup_widget(color,"edit_color");
	*ec = COLOR_NODE;

	gtk_color_selection_set_current_color(colorsel,opts->node_color);
}


void
activateEdgeColor                      (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	GtkWidget *color = (GtkWidget *)user_data;
	GtkColorSelection *colorsel = (GtkColorSelection *)lookup_widget(color,"colorSelection");
	GtkWidget *graph = (GtkWidget *)lookup_widget(color,"graph");
	graph_options *opts = (graph_options *)lookup_widget(graph,"graph_opts");
	short *ec = (short *)lookup_widget(color,"edit_color");
	*ec = COLOR_EDGE;
	gtk_color_selection_set_current_color(colorsel,&opts->edge_color);
}


void
activateLabelColor                     (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	GtkWidget *color = (GtkWidget *)user_data;
	GtkColorSelection *colorsel = (GtkColorSelection *)lookup_widget(color,"colorSelection");
	GtkWidget *graph = (GtkWidget *)lookup_widget(color,"graph");
	graph_options *opts = (graph_options *)lookup_widget(graph,"graph_opts");
	short *ec = (short *)lookup_widget(color,"edit_color");
	*ec = COLOR_LABEL;
	gtk_color_selection_set_current_color(colorsel,opts->label_color);
}


void
load_color                              (GtkButton       *button,
                                        gpointer         user_data)
{
	GtkWidget *color  = (GtkWidget *)user_data;
	GtkWidget *graph = (GtkWidget *)lookup_widget(color,"graph");
	graph_options *opts = (graph_options *)lookup_widget(graph,"graph_opts");
	GtkColorSelection *colorsel = (GtkColorSelection *)lookup_widget(color,"colorSelection");
	short *ecolor = (short *)lookup_widget(color,"edit_color");
	GConfEngine *gc_engine = gconf_engine_get_default();
	GtkWidget *textEntry;
	char value[11];
	GdkColor *tmp;

	switch(*ecolor) {
		case COLOR_NODE:
			gtk_color_selection_get_current_color(colorsel,opts->node_color);
/*
			gconf_engine_set_int(gc_engine,"/apps/w3live/color_node",opts->node_color,NULL);
			textEntry = (GtkWidget *)lookup_widget(color,"entryNodeColor");
			snprintf(value,11,"%x",(unsigned int)opts->node_color);
			gtk_entry_set_text(GTK_ENTRY (textEntry),value);
*/
			break;
		case COLOR_EDGE:
			gtk_color_selection_get_current_color(colorsel,opts->edge_color);
			break;
		case COLOR_LABEL:
			gtk_color_selection_get_current_color(colorsel,opts->label_color);
			break;
	}
	gconf_engine_unref(gc_engine);
}

void
set_color                              (GtkButton       *button,
                                        gpointer         user_data)
{
	GtkWidget *color  = (GtkWidget *)user_data;
	GtkWidget *graph = (GtkWidget *)lookup_widget(color,"graph");
	graph_options *opts = (graph_options *)lookup_widget(graph,"graph_opts");
	GnomeCanvas *canvas = (GnomeCanvas *)lookup_widget(graph,"canvasGraph");
	GraphFrame **graphptr = (GraphFrame **)lookup_widget(graph,"graphptr");
	short *ecolor = (short *)lookup_widget(color,"edit_color");
	if(*ecolor)
		free(ecolor);
	gtk_widget_destroy(color);
	draw_graph(canvas,*graphptr,opts);
}
